<?php
/**
 * 后台用控制器模板
 */
namespace app\admin\controller\teachaffair;

use app\admin\controller\AuthController;
use service\JsonService;


class Teacher extends AuthController
{
	public $model = '\app\common\model\TeacherInfo';

    use \traits\ShotcutControllerTrait;

    /**
     * 新增额外操作 如无留空
     * @Author   FW
     * @DateTime 2021-01-13
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    protected function _save($data)
    {
        if ( empty($data['uid']) ) throw new \Exception('必须绑定会员');
        if ( empty($data['admin_id']) ) throw new \Exception('必须绑定管理员');

        if ( !isset($data['id']) ) {
            ## 检测会员或管理员是否已经绑定过
            $isBind = $this->model::where('admin_id',$data['admin_id'])->count();
            if ( $isBind > 0 ) throw new \Exception('该管理员已被绑定，请选择其他或新建管理员');

            $isBind = $this->model::where('uid',$data['uid'])->count();
            if ( $isBind > 0 ) throw new \Exception('该会员已被绑定，请选择其他');
        }

        return $data;
    }

    public function generateSpreadQr()
    {
        $post = $this->request->post();
        if ( empty($post['id']) || empty($post['uid']) ) return JsonService::fail('操作失败1001');
        //$bg = $post['background'] ?? '';

        $qrData = $post;
        $qrcodeBuffer = \service\WechatMicroProgramService::getWxaCode(sprintf('/pages/index/index?sales_uid=%d',$qrData['uid']));
        $savePath = sprintf('/public/uploads/qrcode/spreaderSales_%d.png',$qrData['uid']);
        file_put_contents(ROOT_PATH . $savePath, $qrcodeBuffer);
        $qrData['spread_qr'] = request()->domain() . $savePath;

        $res = $this->model::where('id',$qrData['id'])->update(['spread_qr' => $qrData['spread_qr']]);
        if ( $res === false ) return JsonService::fail('操作失败1002');

        return JsonService::successful($qrData);
    }
}//Class End
